set more off
clear all
version 17.0

global pc Hannes
global pc hannes.greve
set matsize 10000

global xlist linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks lyeduccook housing_selfowned sexhead agrihead unemphead cook_female d_road elec rural i.religion i.ethnicgroup //i.location
global xlisturban linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks lyeduccook housing_selfowned sexhead agrihead unemphead cook_female i.religion i.ethnicgroup //i.location
global reglist linccap lsize housing_nrooms lagehead lyeduc cook_jobweeks 
global absorblist housing_selfowned sexhead agrihead unemphead cook_female d_road elec location religion ethnicgroup 
global absorblisturban housing_selfowned sexhead agrihead unemphead cook_female location religion ethnicgroup  
global absorblistDID housing_selfowned sexhead agrihead unemphead cook_female d_road elec location religion ethnicgroup region
global absorblisturbanDID housing_selfowned sexhead agrihead unemphead cook_female location religion ethnicgroup region


set seed 22840171
************************************************************************************************
*** Entropy balancing weights *********************************************************************************
************************************************************************************************

*************
*** GLSS6 **********************************************************************
use "$datain\glss_DiDsample.dta", clear
cd "$output"
*keep if glss==6 & (aftercategory==2 | aftercategory==3) 
keep if (glss==6 & (aftercategory==2 | aftercategory==3)) | (glss==5 & eligible5==1)
keep if cooking!=0
drop if cooking==.

*** ATE ***
ebalance treat $xlist if glss==6, keep(ebbalance_ate1) replace targets(2)
*winsor2 _webal, c(0 99.1) trim replace //extreme weights of over 100
replace _webal=. if _webal>=50
gen temp=_webal
gen tag=0 if temp!=.
levelsof location, local(temp)
foreach k of local temp{
	qui sum treat if location==`k' & temp!=.
	qui replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist if tag==0, keep(ebbalance_ate1) replace basewt(temp) targets(2)

preserve
local est ate
cap drop weight_`est'
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

//charcoal intensive
preserve
keep if lq_coal_hat2!=.
cap drop tag2
gen tag2=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' 
			qui replace tag2=1 if location==`k' & d_road==`l' & elec==`t' & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist  if lq_coal_hat2!=. & tag2==0, keep(ebbalance_ate2) replace targets(2)
sum _webal
replace _webal=. if _webal>=50
cap drop temp
cap drop tag
gen temp=_webal
gen tag=0 if temp!=.
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' 
			qui replace tag=1 if location==`k' & d_road==`l' & elec==`t' & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist if lq_coal_hat2!=. & tag==0, keep(ebbalance_ate2) replace basewt(temp) targets(2)
sum _webal
replace _webal=. if _webal>=50
cap drop temp
cap drop tag
gen temp=_webal
gen tag=0 if temp!=.
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' 
			qui replace tag=1 if location==`k' & d_road==`l' & elec==`t' & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist if lq_coal_hat2!=. & tag==0, keep(ebbalance_ate2) replace basewt(temp) targets(2)
sum _webal

local est ate_coal
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

//LPG intensive
preserve
keep if lexp_lpg2!=.
gen tag3=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' 
			qui replace tag3=1 if location==`k' & d_road==`l' & elec==`t' & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist i.location rural elec d_road i.religion if lexp_lpg2!=. & tag3==0, keep(ebbalance_ate3) replace targets(2)

local est ate_lpg
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore



*** rural ***
preserve
keep if rural==1
cap drop tag
gen tag=0
levelsof location, local(temp)
levelsof ethnicgroup, local(temp2)
foreach k of local temp{
	foreach l of local temp2{
		qui sum treat if location==`k' & ethnicgroup==`l'
		replace tag=1 if location==`k'  & (r(mean)==1 | r(mean)==0) & ethnicgroup==`l'
	}
}

ebalance treat $xlist if tag==0, keep(ebbalance_cater) replace targets(2)
sum _webal
replace _webal=. if _webal>=50
cap drop temp
gen temp=_webal
cap drop tag
gen tag=0 if temp!=.
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k' & temp!=.
	qui replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist if tag==0, keep(ebbalance_cater) replace basewt(temp) targets(2)
sum _webal

local est cater
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

/*
preserve //doesnt work
keep if lq_coal_hat2!=. & rural==1
cap drop tag2
gen tag2=0
*gen d_jobweeks=cook_jobweeks>0
*sum d_jobweeks
levelsof location, local(temp)
foreach k of local temp{
	*foreach l in 0 1{
		qui sum treat if location==`k' //& d_jobweeks==`l'
		replace tag2=1 if location==`k' & (r(mean)==1 | r(mean)==0) //& d_jobweeks==`l'
	*}
}
sum tag2
ebalance treat $xlist if tag2==0 & location!=127 & location!=115 
*reghdfe lq_coal_hat2 treat $reglist [aweight=_webal], absorb($absorblist ) vce(robust)
*est store coal3r
restore

preserve //doesnt work
keep if lexp_lpg2!=. & rural==1
cap drop tag3
gen tag3=0
levelsof location , local(temp)
foreach k of local temp{
	qui sum treat if location==`k' 
	replace tag3=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist if tag3==0
*reghdfe lexp_lpg2 treat $reglist [aweight=_webal], absorb($absorblist ) vce(robust)
*est store lpg3r
restore*/


*** urban ***
cap drop temp
cap drop tag
ebalance treat $xlisturban if rural==0, keep(ebbalance_cateu1) replace targets(2)
sum _webal
replace _webal=. if _webal>=50
gen temp=_webal
gen tag=0
levelsof location, local(temp)
foreach k of local temp{
	qui sum treat if location==`k' & temp!=.
	qui replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist if tag==0 & rural==0, keep(ebbalance_cateu1) replace basewt(temp) targets(2)
sum _webal


preserve
local est cateu
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore


preserve
keep if lq_coal_hat2!=. & rural==0
cap drop tag2
gen tag2=0
levelsof location, local(temp)
foreach k of local temp{
	qui sum treat if location==`k' 
	qui replace tag2=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlisturban if tag2==0, keep(ebbalance_cateu2) replace targets(2)
sum _webal
cap drop temp
cap drop tag
replace _webal=. if _webal>=50
gen temp=_webal
gen tag=0 if tag2==0
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k' & temp!=.
	qui replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist if tag==0 & rural==0, keep(ebbalance_cateu2) replace basewt(temp) targets(2)
sum _webal

local est cateu_coal
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

preserve
keep if lexp_lpg2!=. & rural==0
cap drop tag3
gen tag3=0
levelsof location , local(temp)
foreach k of local temp{
	qui sum treat if location==`k' 
	qui replace tag3=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlisturban if tag3==0, keep(ebbalance_cateu3) replace targets(2)
local est cateu_lpg
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore




*************
*** GLSS5 **********************************************************************
use "$datain\glss_DiDsample.dta", clear
keep if cooking!=0
drop if cooking==.
keep if glss==5 & eligible5==1
drop religion
rename religion5 religion

gen tag=0
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k'
	replace tag=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist if tag==0, keep(ebbalance5_ate1) replace targets(2)
sum _webal
replace _webal=. if _webal>=50
gen temp=_webal
gen tag2=0 if tag==0
levelsof location, local(temp)
foreach k of local temp{
	sum treat if location==`k' & temp!=.
	replace tag2=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist if tag2==0, keep(ebbalance5_ate1) replace basewt(temp) targets(2)
sum _webal

preserve
local est ate5
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

//charcoal intensive
preserve
keep if lq_coal_hat2!=.
cap drop tag2
gen tag2=0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		foreach t in 0 1{
			qui sum treat if location==`k' & d_road==`l' & elec==`t' 
			replace tag2=1 if location==`k' & d_road==`l' & elec==`t' & (r(mean)==1 | r(mean)==0)
		}
	}
}
ebalance treat $xlist  if lq_coal_hat2!=. & tag2==0, keep(ebbalance5_ate2) replace targets(2)
sum _webal

local est ate5_coal
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore


preserve
keep if lexp_lpg2!=.
gen tag3=0
levelsof location, local(temp)
foreach k of local temp{
	qui sum treat if location==`k' 
	replace tag3=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlist i.location rural elec d_road i.religion if lexp_lpg2!=. & tag3==0, targets(2) keep(ebbalance5_ate3) replace
local est ate5_lpg
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore



*** rural *** 
preserve
keep if rural==1
cap drop tag
cap drop tag2 
cap drop temp
gen tag=0
levelsof location, local(temp)
levelsof ethnicgroup, local(temp2)
foreach k of local temp{
	foreach l of local temp2{
		qui sum treat if location==`k' & ethnicgroup==`l'
		replace tag=1 if location==`k' & ethnicgroup==`l' & (r(mean)==1 | r(mean)==0)
	}
}

ebalance treat $xlist if tag==0 , keep(ebbalance5_cater) replace 
gen temp=_webal
gen tag2=0 if tag==0
levelsof location, local(temp)
levelsof ethnicgroup, local(temp2)
foreach k of local temp{
	foreach l of local temp2{
		qui sum treat if location==`k' & ethnicgroup==`l'
		replace tag2=1 if location==`k' & ethnicgroup==`l' & (r(mean)==1 | r(mean)==0)
	}
}
ebalance treat $xlist if tag2==0, keep(ebbalance5_cater) replace basewt(temp)
local est cater5
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

/*
preserve //doesnt work
keep if lq_coal_hat2!=. & rural==1
cap drop tag2
gen tag2=0
*gen d_jobweeks=cook_jobweeks>0
*sum d_jobweeks
levelsof location, local(temp)
foreach k of local temp{
	*foreach l in 0 1{
		qui sum treat if location==`k' //& d_jobweeks==`l'
		replace tag2=1 if location==`k' & (r(mean)==1 | r(mean)==0) //& d_jobweeks==`l'
	*}
}
sum tag2
ebalance treat $xlist if tag2==0 & location!=127 & location!=115
*reghdfe lq_coal_hat2 treat $reglist [aweight=_webal], absorb($absorblist ) vce(robust)
*est store coal3r
restore

preserve //doesnt work
keep if lexp_lpg2!=. & rural==1
cap drop tag3
gen tag3=0
levelsof location , local(temp)
foreach k of local temp{
	qui sum treat if location==`k' 
	replace tag3=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
cap noisily ebalance treat $xlist if tag3==0
*reghdfe lexp_lpg2 treat $reglist [aweight=_webal], absorb($absorblist ) vce(robust)
*est store lpg3r
restore*/


*** urban ***
ebalance treat $xlisturban if tag==0 & rural==0, keep(ebbalance5_cateu1) replace  targets(2)

preserve
local est cateu5
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

preserve
keep if lq_coal_hat2!=. & rural==0
cap drop tag2
gen tag2=0
levelsof location, local(temp)
foreach k of local temp{
	qui sum treat if location==`k' 
	replace tag2=1 if location==`k' & (r(mean)==1 | r(mean)==0)
}
ebalance treat $xlisturban if tag2==0, keep(ebbalance5_cateu2) replace targets(2)
local est cateu5_coal
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

preserve
keep if lexp_lpg2!=. & rural==0
cap drop tag3
gen tag3=0
gen d_jobweeks=cook_jobweeks>0
levelsof location, local(temp)
foreach k of local temp{
	foreach l in 0 1{
		qui sum treat if location==`k' & d_jobweeks==`l'
		replace tag3=1 if location==`k' & (r(mean)==1 | r(mean)==0) //& d_jobweeks==`l'
	}
}
ebalance treat $xlisturban if tag3==0, targets(2) keep(ebbalance5_cateu3) replace
local est cateu5_lpg
gen weight_`est'=_webal
keep hid weight_`est'
save "$output\weight_`est'.dta", replace
restore

//merge weights
clear all
use "$output\weight_ate"
foreach est in ate_coal ate_lpg cater cateu cateu_coal cateu_lpg ///
	 ate5 ate5_coal ate5_lpg cater5 cateu5 cateu5_coal cateu5_lpg{
	merge 1:1 hid using "$output\weight_`est'"
	drop _merge
}
save "$output\weights_eb.dta", replace

foreach est in ate ate_coal ate_lpg cater cateu cateu_coal cateu_lpg ///
	 ate5 ate5_coal ate5_lpg cater5 cateu5 cateu5_coal cateu5_lpg{
	erase "$output\weight_`est'.dta"
}

//check balance
preserve
foreach file in ate1 ate2 ate3 cater cateu1 cateu2 cateu3{
	use ebbalance_`file', clear
	replace sdiff_Post=sdiff_Post*(-1) if sdiff_Post<0
	di "************ `file' ************"
	sum sdiff_Post
	gen varratio=var_Tr/var_Co_Post
	sum varratio if varratio!=0
}
restore

preserve
foreach file in ate1 ate2 ate3 cater cateu1 cateu2 cateu3{
	use ebbalance5_`file', clear
	replace sdiff_Post=sdiff_Post*(-1) if sdiff_Post<0
	di "************ `file' ************"
	sum sdiff_Post
	gen varratio=var_Tr/var_Co_Post
	sum varratio if varratio!=0
}
restore




************************************************************************************************
*** CBPS ***************************************************************************************
************************************************************************************************

*************
*** GLSS6 **********************************************************************
cap log close

log using "$output\CBPS.txt", replace
use "$datain\glss_DiDsample.dta", clear
keep if glss==6 & (aftercategory==2 | aftercategory==3) 
keep if cooking!=0
drop if cooking==.
****** Covariate Balancing Propensity Score ************************************
*** full sample ***
//full sample
psweight cbps treat $xlist, iter(500) atet
psweight call balanceresults()
gen weight_ate1=_weight

//only charcoal users
psweight cbps treat $xlist if lq_coal_hat2!=., iter(500) atet
psweight call balanceresults()
gen weight_ate2=_weight

//only LPG users
psweight cbps treat $xlist if lexp_lpg2!=., iter(500) atet
psweight call balanceresults()
gen weight_ate3=_weight

*** urban sample***
//full sample
psweight cbps treat $xlisturban if rural==0, iter(500) atet
psweight call balanceresults()
gen weight_cateu1=_weight

//only charcoal users
psweight cbps treat $xlisturban if lq_coal_hat2!=. & rural==0, iter(500) atet
psweight call balanceresults()
gen weight_cateu2=_weight

//only LPG users
psweight cbps treat $xlisturban if lexp_lpg2!=. & rural==0, iter(500) atet
psweight call balanceresults()
gen weight_cateu3=_weight

*** rural sample***
//full sample
psweight cbps treat $xlist if rural==1, iter(500) atet
psweight call balanceresults()
gen weight_cater1=_weight

//only charcoal users - DOESNT WORK
*psweight cbps treat $xlist if lq_coal_hat2!=. & rural==1, iter(500)
*psweight call balanceresults()
*gen weight_cater2=_weight

//only LPG users - DOESNT WORK
*psweight cbps treat $xlist if lexp_lpg2!=. & rural==1, iter(500)
*psweight call balanceresults()
*gen weight_cater3=_weight

preserve
keep hid weight_*
save "$output\weights_cbps.dta", replace
restore
cap log close

*************
*** GLSS5 **********************************************************************
cap log close
log using "$output\CBPS5.txt", replace
use "$datain\glss_DiDsample.dta", clear
keep if glss==5 & eligible5==1
keep if cooking!=0
drop if cooking==.
drop religion
rename religion5 religion
****** Covariate Balancing Propensity Score ************************************
*** full sample ***
//full sample
psweight cbps treat $xlist, iter(500) atet
psweight call balanceresults()
gen weight_ate1=_weight

//only charcoal users
psweight cbps treat $xlist if lq_coal_hat2!=., iter(500) atet
psweight call balanceresults()
gen weight_ate2=_weight

//only LPG users
psweight cbps treat $xlist if lexp_lpg2!=., iter(500) atet
psweight call balanceresults()
gen weight_ate3=_weight

*** urban sample***
//full sample
psweight cbps treat $xlisturban if rural==0, iter(500) atet
psweight call balanceresults()
gen weight_cateu1=_weight

//only charcoal users
psweight cbps treat $xlisturban if lq_coal_hat2!=. & rural==0, iter(500) atet
psweight call balanceresults()
gen weight_cateu2=_weight

//only LPG users
psweight cbps treat $xlisturban if lexp_lpg2!=. & rural==0, iter(500) atet
psweight call balanceresults()
gen weight_cateu3=_weight

*** rural sample***
//full sample
psweight cbps treat $xlist if rural==1, iter(500) atet
psweight call balanceresults()
gen weight_cater1=_weight

//only charcoal users - DOESNT WORK
*psweight cbps treat $xlist if lq_coal_hat2!=. & rural==1, iter(500)
*psweight call balanceresults()
*gen weight_cater2=_weight

//only LPG users - DOESNT WORK
*psweight cbps treat $xlist if lexp_lpg2!=. & rural==1, iter(500)
*psweight call balanceresults()
*gen weight_cater3=_weight

preserve
keep hid weight_*
tempfile temp
save `temp', replace
use "$output\weights_cbps.dta", clear
append using `temp'
save "$output\weights_cbps.dta", replace
restore
cap log close

